package com.jackie.tongzhuang.Controls;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jackie.tongzhuang.Classes.BaseClass;
import com.jackie.tongzhuang.DataBase.DbClass;
import com.jackie.tongzhuang.R;

public class GongHuoShangControl extends LinearLayout implements View.OnClickListener {

    private TextView mctext,dztext,lxdhtext;
    private ImageView editimg,delimg;

    private String mc,dz,lxdh;
    private Drawable edit,del;
    private int ghsid,rowid;
    private Context context;
    private DbClass dbClass;

    public GongHuoShangControl(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        dbClass = new DbClass(context);
        View view = LayoutInflater.from(context).inflate(R.layout.control_gonghuoshangcard,this);
        mctext = view.findViewById(R.id.control_ghs_mc);
        dztext = view.findViewById(R.id.control_ghs_dz);
        lxdhtext = view.findViewById(R.id.control_ghs_lxdh);
        editimg = view.findViewById(R.id.control_ghs_edit);
        delimg = view.findViewById(R.id.control_ghs_delete);

        delimg.setOnClickListener(this);
        editimg.setOnClickListener(this);

        TypedArray t = context.obtainStyledAttributes(attrs,R.styleable.FenQiList);
        mc = t.getString(R.styleable.GongHuoShangControl_mcText);
        dz = t.getString(R.styleable.GongHuoShangControl_dzText);
        lxdh = t.getString(R.styleable.GongHuoShangControl_lxdhText);
        del = t.getDrawable(R.styleable.GongHuoShangControl_delSrc);
        edit = t.getDrawable(R.styleable.GongHuoShangControl_editSrc);
        ghsid = t.getInt(R.styleable.GongHuoShangControl_ghsId,0);

        if(mc != null){
            mctext.setText(mc);
        }

        if(dz != null){
            dztext.setText(dz);
        }

        if(lxdh != null){
            lxdhtext.setText(lxdh);
        }

        if(edit != null){
            editimg.setImageDrawable(edit);
        }

        if(del != null){
            delimg.setImageDrawable(del);
        }


        t.recycle();

    }

    public void setGongHuoShangId(int ghsid){
        this.ghsid = ghsid;
    }

    public int getGongHuoShangId(){
        return this.ghsid;
    }

    public void setRowId(int rowid){
        this.rowid = rowid;
    }

    public int getRowId(){
        return rowid;
    }

    public String getMingCheng() {
        return mc;
    }

    public void setMingCheng(String mc) {
        this.mc = mc;
        mctext.setText(mc);
    }

    public String getDiZhi() {
        return dz;
    }

    public void setDiZhi(String dz) {
        this.dz = dz;
        dztext.setText(dz);
    }

    public String getLianXiDianHua() {
        return lxdh;
    }

    public void setLianXiDianHua(String lxdh) {
        this.lxdh = lxdh;
        lxdhtext.setText(lxdh);
    }

    public Drawable getEditImg() {
        return edit;
    }

    public void setEditImg(Drawable edit) {
        editimg.setImageDrawable(edit);
        this.edit = edit;
    }

    public Drawable getDelImg() {
        return del;
    }

    public void setDelImg(Drawable del) {
        this.del = del;
        delimg.setImageDrawable(del);
    }

    public void deleteSelf(){
        ViewGroup parent = (ViewGroup) getParent();
        if (parent != null) {
            parent.removeView(this);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.control_ghs_delete:
                AlertDialog dialog = new AlertDialog.Builder(context).create();
                dialog.setTitle("删除提示");
                dialog.setMessage("删除此供货商将导致某些表数据不可用，请谨慎操做！");
                dialog.setButton(DialogInterface.BUTTON_POSITIVE,"确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Log.v("msgg","del"+ghsid);
                        dbClass.delete("GongHuoShang",ghsid);
                        deleteSelf();
                        dialog.dismiss();
                    }
                });
                dialog.setButton(DialogInterface.BUTTON_NEGATIVE,"取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialog.dismiss();
                    }
                });
                dialog.show();
                break;
            case R.id.control_ghs_edit:
                Log.v("msgg","edit"+ghsid);
                triggerParentEvent();
                break;
        }
    }

    public interface EditClickListener{
        void editClick(int ghsid,int rowid);
    }

    public EditClickListener listener;

    public void setOnClickedListener(EditClickListener _listener){
        listener = _listener;
    }

    public void triggerParentEvent() {
        if (listener != null) {
            listener.editClick(ghsid,rowid);
        }
    }

}
